package com.example.lib_cache.viewmodel;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.lib_cache.model.Application;
import com.example.lib_cache.model.OrderNavItem;
import com.example.lib_cache.model.OrderWithNav;
import com.example.lib_cache.model.User;
import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_cache.network.exception.ErrorConsumer;
import com.example.lib_cache.network.response.ResponseResultTransformer;
import com.example.lib_cache.service.ApiService;
import com.netease.nim.uikit.common.util.C;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class OrderNavActivityViewModel extends ViewModel {

    private final static String TAG = OrderNavActivityViewModel.class.getSimpleName();


    private MutableLiveData<List<OrderNavItem>> orderNavItems = new MutableLiveData<>();
    private MutableLiveData<List<OrderWithNav>> orderWithNavLiveData = new MutableLiveData<>();
    private MutableLiveData<Boolean> result = new MutableLiveData<>();
    private MutableLiveData<User> userMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<Application> applicationMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<User> getUserMutableLiveData() {
        return userMutableLiveData;
    }

    public void setUserMutableLiveData(User user) {
        this.userMutableLiveData.postValue(user);
    }

    public MutableLiveData<Application> getApplicationMutableLiveData() {
        return applicationMutableLiveData;
    }

    public void setApplicationMutableLiveData(Application application) {
        this.applicationMutableLiveData.postValue(application);
    }

    public MutableLiveData<Boolean> getResult() {
        return result;
    }

    public void setResult(Boolean result) {
        this.result.postValue(result);
    }

    public MutableLiveData<List<OrderWithNav>> getOrderWithNavLiveData() {
        return orderWithNavLiveData;
    }

    public void setOrderWithNavLiveData(List<OrderWithNav> orderWithNavs) {
        this.orderWithNavLiveData.postValue(orderWithNavs);
    }

    public MutableLiveData<List<OrderNavItem>> getOrderNavItems() {
        return orderNavItems;
    }

    public void setOrderNavItems(List<OrderNavItem> list) {

        this.orderNavItems.postValue(list);
    }

    public List<OrderNavItem> toOrderNavItem(List<OrderWithNav> orderWithNavs){
        List<OrderNavItem> list = new ArrayList<>();
//        Log.d(TAG, "列表长度"+orderWithNavs.size());

        for (OrderWithNav o:orderWithNavs) {
//            Log.d(TAG, "name1"+o.getsName());
//            Log.d(TAG, "name2"+o.geteName());


            OrderNavItem orderNavItem = new OrderNavItem();
            orderNavItem.setDriverId(o.getDeliveryDriver());
            orderNavItem.setStart(o.getsName());
            orderNavItem.setEnd(o.geteName());
            orderNavItem.setData(o.getPublishingTime());
            orderNavItem.setStatus(o.getStatus());
            orderNavItem.setCount(o.getPassengerNumber());
            list.add(orderNavItem);
        }
        return list;
    }

    public void requestHistoryOrders(ApiService apiService, Context context){
        apiService.getHistoryOrders()
                .compose(ResponseResultTransformer.<List<OrderWithNav>>obtain())
                .subscribe(new Consumer<List<OrderWithNav>>() {
                    @Override
                    public void accept(List<OrderWithNav> orderWithNavs) throws Exception {
                        Log.d(TAG,"订单获取成功");

                        Log.d(TAG, orderWithNavs.get(0).getsName());

                        setOrderWithNavLiveData(orderWithNavs);
//                        setOrderWithNavs(orderWithNavs);
//                        setOrderNavItems(orderWithNavs);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"请求失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                        if("201".equals(ex.getCode())){
                            setResult(false);
                        }
                    }
                });
    }

    public void requestApplication(ApiService apiService,Context context,Long driverId){
        apiService.getDriverCarInfo(driverId)
                .compose(ResponseResultTransformer.<Application>obtain())
                .subscribe(new Consumer<Application>() {
                    @Override
                    public void accept(Application application) throws Exception {
                        Log.d(TAG,"请求成功");
                        setApplicationMutableLiveData(application);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"请求失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                        if("201".equals(ex.getCode())){
                            setResult(false);
                        }
                    }
                });
    }

    public void requestPersonInfo(ApiService apiService,Context context,Long userId){
        apiService.getUser(userId).compose(ResponseResultTransformer.<User>obtain())
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        Log.d(TAG,"请求成功");
                        setUserMutableLiveData(user);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"请求失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                        if("201".equals(ex.getCode())){
                            setResult(false);
                        }
                    }
                });
    }


}
